//
//  DMRegularExpression.swift
//  DWater-Swift
//
//  Created by mac on 2017/4/17.
//  Copyright © 2017年 Dimon.胡亚宾. All rights reserved.
//

import UIKit

class DMRegularExpression: NSObject {

    /// 正则匹配邮箱号
    ///
    /// - Parameter mail: email
    /// - Returns: 正则对不对
    open class func checkMailInput(mail:String) -> Bool {
     
        let mailPattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let matcher = MyRegex(mailPattern)
        let mailAddress = mail
        return matcher.match(input: mailAddress)
        
    }
    

    
    /// 验证新手机号
    ///
    /// - Parameter telNumber: <#telNumber description#>
    /// - Returns: <#return value description#>
    open class func checkNewTelNumber(telNumber:String) -> Bool {

        let mailPattern = "^(13[0-9]|17[0-9]|15[0-9]|14[0-9]|18[0-9])\\d{8}$"
        let matcher = MyRegex(mailPattern)
        let maybeMailAddress = telNumber
        return matcher.match(input: maybeMailAddress)
        
    }

    ///
    ///
    /// - Parameter adress: 详细地址{2-80}位
    /// - Returns: <#return value description#>
    open class func checkAdressDetail(adress:String) -> Bool {
        
        let adress = adress.replacingOccurrences(of: " ", with: "")
        let adressPattern = "[\\dA-Za-z\u{4E00}-\u{9FA5}]"
        let matcher = MyRegex(adressPattern)
        return matcher.match(input: adress)
        
    }
    
    /// 正则匹配用户密码6-18位数字和字母组合
    ///
    /// - Parameter password: 密码
    /// - Returns:
    open class func checkPassword(password:String) -> Bool {

        let adressPattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}"
        let matcher = MyRegex(adressPattern)
        return matcher.match(input: password)
        
        
    }
    
    
    /// 正则匹配用户姓名,20位的中文或英文
    ///
    /// - Parameter userName: 用户名
    /// - Returns:
    open class func checkUserName(userName:NSString) -> Bool {
      
        let uesrNames = userName.replacingOccurrences(of: " ", with: "")
        let adressPattern = "[\\dA-Za-z\u{4E00}-\u{9FA5}]{2,10}"
        let matcher = MyRegex(adressPattern)
        return matcher.match(input: uesrNames)
    }
    
    ///  正则匹配用户身份证号15或18位
    ///
    /// - Parameter idCard: <#idCard description#>
    /// - Returns: <#return value description#>
    open class func checkUserIdCard(idCard:String) -> Bool {
        var flag : Bool?
        if (idCard.characters.count <= 0) {
            flag = false
            return flag!
        }
        
        let adressPattern = "^(\\d{14}|\\d{17})(\\d|[xX])$"
        let matcher = MyRegex(adressPattern)
        return matcher.match(input: idCard)
        
    }
    
    open class func checkEmployeeNumber(number:NSString) -> Bool {
        
        return true

    }
    open class func checkURL(url:NSString) -> Bool {
        
        return true
 
    }
    open class func checkNickname(nickname:NSString) -> Bool {
        
        return true

    }
    open class func checkCtooNumberTo18(nickNumber:NSString) -> Bool {
        
        return true
 
    }
    open class func checkCtooNumber(nickNumber:NSString) -> Bool {
        
        return true
 
    }
    
    open class func checkCtooNumberTo(nickNumber:NSString) -> Bool {
        
        return true
 
    }
    open class func checkBankNumber(bankNumber:NSString) -> Bool {
        
        return true
  
    }
    open class func checkCheJiaNumber(CheJiaNumber:NSString) -> Bool {
        
        return true

    }
    open class func checkTeshuZifuNumber(CheJiaNumber:NSString) -> Bool {
        
        return true
  
    }
    
    open class func checkCarNumber(CarNumber:NSString) -> Bool {
        
        return true

    }
    
}


struct MyRegex {
    let regex: NSRegularExpression?
    
    init(_ pattern: String) {
        regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive)
    }
    
    func match(input: String) -> Bool {
        if let matches = regex?.matches(in: input,
                                                options: [],
                                                range: NSMakeRange(0, (input as NSString).length)) {
            return matches.count > 0
        }
        else {
            return false
        }
    }
}

